package gtc.edu.ybt.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import gtc.edu.ybt.demo.entity.Course;
import gtc.edu.ybt.demo.entity.Grade;
import gtc.edu.ybt.demo.entity.Student;
import gtc.edu.ybt.demo.entity.SysUser;
import gtc.edu.ybt.demo.entity.TbClass;
import gtc.edu.ybt.demo.security.UserUtils;
import gtc.edu.ybt.demo.service.CourseService;
import gtc.edu.ybt.demo.service.GradeService;
import gtc.edu.ybt.demo.service.StudentService;
import gtc.edu.ybt.demo.service.TbClassService;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private CourseService courseService;



	@RequestMapping(value = "/bjcjlr")
	public String bjcjlr(ModelMap modelMap) {
		List<TbClass> tbClasses = tbClassService.findAll();
		List<Course> courses = courseService.findAll();
		modelMap.put("bjs", tbClasses);
		modelMap.put("kcs", courses);
		return "grade/bjcjlr";
	}

	@RequestMapping(value = "/getClassGrade")
	public String bjcj(Integer bjId, Integer kcId, ModelMap modelMap) {
		List<Student> students = studentService.findByTbClassId(bjId);
		List<Grade> grades = new ArrayList<Grade>();
		for (Student s : students) {
			Grade grade = gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
			if (grade == null) {
				Grade grade1 = new Grade();
				grade1.setCourse(courseService.findById(kcId));
				grade1.setStudent(studentService.findById(s.getId()));
				gradeService.save(grade1);
				grades.add(grade1);
			} else {
				grades.add(grade);
			}
		}
		modelMap.put("grades", grades);
		return "grade/getClassGrade";

	}

	@RequestMapping(value = "/bjcjcx")
	public String list(ModelMap modelMap) {
		List<TbClass> tbClass=tbClassService.findAll();
		modelMap.put("list",tbClass);
		return "grade/bjcjcx";
	}

	@RequestMapping(value = "bjChange")
	public String bjChange(Integer bjId, ModelMap modelMap) {
		List<Course> courses = gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId, courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		return "grade/cjb";

	}

	@RequestMapping(value = "/cjChange")
	@ResponseBody
	public void cjChange(Integer id, Integer grade) {
		Grade g = gradeService.findById(id);
		g.setGrade(grade);
		gradeService.save(g);

	}

	@RequestMapping(value = "/cjChange1")
	@ResponseBody
	public void cjChange1(Integer id, Integer grade) {
		Grade grade2 = gradeService.findById(id);
		grade2.setGrade(grade);
		gradeService.save(grade2);
	}

	@RequestMapping(value = "/getstu")
	public void getStu(Integer bjId, ModelMap modelMap) {
		List<Student> students = studentService.findByTbClassId(bjId);
		modelMap.put("list", students);
	}

	@RequestMapping(value = "/grcjlr")
	public void personGradelr(ModelMap modelMap) {
		List<TbClass> tbClasses = tbClassService.findAll();
		modelMap.put("list", tbClasses);

	}

	@RequestMapping(value = "/getPersonGrade")
	public void getPersonGrade(Integer stuId, ModelMap modelMap) {
		List<Grade> grades = gradeService.findByStudentId(stuId);
		modelMap.put("list", grades);
	}

	@RequestMapping(value = "/peasonGrade")
	public String personGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/peasonGrade";
	}

	@RequestMapping(value = "/xsszbjcjcx")
	public String calssGrade( ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		Student student=studentService.findById(id);
		Integer bjId=student.getTbClass().getId();
		List<Course> courses = gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb = gradeService.getCjb(bjId, courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		return "grade/xsszbjcjcx";

	}
}
	